package com.app.backend.activiti.listener;

import com.app.backend.activiti.entity.biz.Leave;
import com.app.backend.activiti.service.LeaveService;
import com.app.backend.common.utils.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;

/**
 * @program: MyExecutionListener
 * @description:
 * @author: Zhun.Xiao
 * @create: 2020-03-04 09:09
 **/
@Slf4j
public class MyExecutionListener implements ExecutionListener {
    @Override
    public void notify(DelegateExecution execution) throws Exception {

        // 获取关联业务表ID , start流程时已经put
        String tableId = (String) execution.getVariable("tableId");
        log.info(tableId);
        log.info("=== MyExecutionListener tableId:{}", tableId);

        //获取对应申请种类
        String bizName = execution.getProcessBusinessKey();
        String eventName = execution.getEventName();
        log.info("申请流程为 {}", bizName);
        log.info(eventName);
        if ("leave".equals(bizName)) {
            LeaveService leaveService = SpringContextUtil.getBean(LeaveService.class);
            Leave leave = leaveService.get(tableId);

            //将对应业务表中的申请数据填充进流程变量中
            execution.setVariable("duration", leave.getDuration());

            if (EVENTNAME_START.equals(eventName)) {

            } else if (EVENTNAME_END.equals(eventName)) {

            }
        } else if ("xxx".equals(bizName)) {
            log.info("=== 其他申请流程{}", "xxx");

        }


    }
}